<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UserResource extends AdminController{
	public $_validation = null;
	function __construct(){
		parent::__construct();
		$this->load->model('MAdmin');
		$this->_validation = Array();
	}

	function index(){
		$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		
		$this->formfilter->addFilter('a.id', 'where');
		$this->formfilter->addFilter('a.account','where');
		$this->formfilter->addFilter('a.name','where');
		
		$limit = $this->pagination($this->MAdmin->getLeaverCount());
		$list = $this->MAdmin->getLeaverList($limit);

		$this->assign('list', $list);
		$this->layout();
	}
	
	/**
	 * 平均分配离职人员的asin给其他在职人员
	 */
	function averageasin($id){
	    $redirectUrl = site_url($this->_thisModule.$this->_thisController);
	    if($this->_user->role_id != 1){
	        errorAndRedirect('没有权限',$redirectUrl);
	    }
	    
	    $counter = 0;
	    //得到所有非离职测评人员
	    $allOfficeAdmins = $this->MAdmin->getAll(array('forbidden'=>0,'role_id'=>3));
	    $allOfficeAdminsNum = count($allOfficeAdmins);
	    
	    //当前人员的所有asin
	    $this->load->model('MAsin');
	    while($asin = $this->MAsin->getOne(array('creator_id'=>$id))){
	        $asin->creator_id = $allOfficeAdmins[$counter]->id;
	        $this->MAsin->update($asin);
	        $counter ++;
	        if($counter == $allOfficeAdminsNum){
	            $counter = 0;
	        }
	    }
	    
	    successAndRedirect('平均分配asin成功！',$redirectUrl);
	}
	/**
	 * 平均分配离职人员的email给其他在职人员
	 */
	function averageemail($id){
	    $redirectUrl = site_url($this->_thisModule.$this->_thisController);
	    if($this->_user->role_id != 1){
	        errorAndRedirect('没有权限',$redirectUrl);
	    }
	    
	    $counter = 0;
	    //得到所有非离职测评人员
	    $allOfficeAdmins = $this->MAdmin->getAll(array('forbidden'=>0,'role_id'=>3));
	    $allOfficeAdminsNum = count($allOfficeAdmins);
	    
	    //当前人员的所有asin
	    $this->load->model('MEmail');
	    while($email = $this->MEmail->getOne(array('creator_id'=>$id))){
	        $email->creator_id = $allOfficeAdmins[$counter]->id;
	        $this->MEmail->update($email);
	        $counter ++;
	        if($counter == $allOfficeAdminsNum){
	            $counter = 0;
	        }
	    }
	    
	    successAndRedirect('平均分配email成功！',$redirectUrl);
	}

}